using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using GameEngine;

namespace GameEngine
{
    public class Display : IDisplay
    {
        internal static Device grfDevice;
        private Vector2D limits;
        private Vector2D cameraPosition;

        public const int HRES = 800;
        public const int VRES = 600;

        public const int TILESIZE = 48;
        public const int FILEX = 16;

        public Display()
        {
            //****** Fullscreen initialisation ******//
            PresentParameters pres = new PresentParameters();
            pres.Windowed = true;
            pres.EnableAutoDepthStencil = false;
            pres.AutoDepthStencilFormat = DepthFormat.D16Lockable;
            pres.BackBufferWidth = 800;
            pres.BackBufferHeight = 600;
            pres.BackBufferFormat = Format.X8R8G8B8;
            pres.PresentationInterval = PresentInterval.Default;
            pres.SwapEffect = SwapEffect.Discard;

            grfDevice = new Device(0, DeviceType.Hardware, WindowsForm.GetWindowsForm().Handle, CreateFlags.SoftwareVertexProcessing, pres);
            grfDevice.Clear(ClearFlags.Target, Color.White.ToArgb(), 1.0F, 0);

            System.Windows.Forms.Cursor.Hide();
            cameraPosition = new Vector2D(10, 10);
        }


        public void Begin()
        {
            grfDevice.Clear(ClearFlags.Target, Color.White.ToArgb(), 1.0F, 0);
            grfDevice.BeginScene();
        }

        public void End()
        {
            grfDevice.EndScene();
            grfDevice.Present();
        }

               
        public void SetCameraPosition(Vector2D position)
        {
            cameraPosition.Set(position.GetX(), position.GetY());
        }

        public Vector2D GetCameraPosition()
        {
            return cameraPosition;
        }

        public void Terminate()
        {
            grfDevice.Dispose();
        }

        public void SetLimits(Vector2D limits)
        {
            this.limits = limits;
        }

        public Vector2D GetLimits()
        {
            return limits;
        }
        
    }
    public class Vector2D
    {
        private float x, y;
        
        public Vector2D()
        {
            x = y = 0.0f;
        }

        public Vector2D(float x, float y)
        {
            this.x = x;
            this.y = y;
        }

        public float GetX()
        {
            return x;
        }

        public float GetY()
        {
            return y;
        }

        public void Set(float x, float y)
        {
            this.x = x;
            this.y = y;
        }

        public static float Distance(Vector2D vect1, Vector2D vect2)
        {
            return (float)Math.Sqrt(Math.Pow(vect1.x - vect2.x, 2) + Math.Pow(vect1.y - vect2.y, 2));
        }
    }
}
